﻿function onSubmitSuccess() {
    if ($("#singleValue").val() != "") {
        $("#greaterThan").val("");
        $("#lessThan").val("");
    }
    $('#submitForm').attr("disabled", true);
    $('#submitForm').val("Saved");
    $(document).click(function() {
        $('#submitForm').val("Save");
        $('#submitForm').attr("disabled", false);
        $(document).unbind('click');
    });
}
$(document).ready(function() {
    $('#submitForm').click(function() {
        if ($("#singleValue").val() != "") {
            $("#greaterThan").val(parseFloat($("#singleValue").val()) - 1);
            $("#lessThan").val(parseFloat($("#singleValue").val()) + 1);
        }
    });
    $('#greaterThan').click(function() {
        $("#singleValue").val("");
    });
    $('#lessThan').click(function() {
        $("#singleValue").val("");
    });
    $('.optionTextarea').each(function() {
        $(this).click(function() {
            $(this).css("border", "solid 1px #7F9DB9");
            $(this).css("background", "#FFFFFF");
            if ($(this).val().length > 40) {
                $(this).parent().parent().css("min-height", "160px");
                $(this).css("height", "160px");
            }
            $(this).blur(function() {
                $(this).css("border", "none");
                $(this).css("background", "none");
                $(this).parent().parent().css("min-height", "15px");
                $(this).css("height", "17px");
            });
        });
    });
    $('.deleteOption').each(function(index) {
        $(this).click(function() {
            if ($('#question').val() == "") {
                return false;
            }
            $.ajax({
                type: 'POST',
                url: "Update" + $('#type').val(),
                data: $('#form0').serialize(),
                success: function(response) {
                    $.ajax({
                        type: 'POST',
                        url: "DeleteAnswerString",
                        data: { questionID: $('#questionID').val(), index: index },
                        success: function(response) {
                            window.location.href = "Edit" + $('#type').val() + "?questionID=" + $('#questionID').val() + "&questionNumber=" + $('#questionNumber').val() + "&pollID=" + $('#pollID').val() + "&pollName=" + $('#pollName').val() + "&saved=true";
                        }
                    });
                }
            });
        });
    });
    $('#submitForm').blur(function() {
        $('#errorMessage').empty();
        $('#result').empty();
    });
    $('#addOptionText').click(function() {
        $(this).css("height", "160px");
        $(this).blur(function() {
            $(this).css("height", "20px");
        });
    });
    $('#addOptionButton').click(function() {
        if ($('#question').val() == "") {
            if ($('#questionID').val() < 0 && $('#question_validationMessage').text() == "") {
                $('#errorMessage').text("Question cannot be blank.");
                $(this).blur(function() {
                    $('#errorMessage').text("");
                    $(this).unbind('blur');
                });
            }
            return false;
        }
        if ($('#addOptionText').val() == "") {
            $('#errorMessage').text("Answer cannot be blank.");
            $(this).blur(function() {
                $('#errorMessage').text("");
                $(this).unbind('blur');
            });
            return false;
        }
        if ($('#questionID').val() == "-1") {
            $.ajax({
                type: 'POST',
                url: "Add" + $('#type').val(),
                data: $('#form0').serialize(),
                success: function(response) {
                    $('#questionID').val(response);
                    $.ajax({
                        type: 'POST',
                        url: "AddAnswerString",
                        data: { questionID: response, answer: $('#addOptionText').val() },
                        success: function(response) {
                            window.location.href = "Edit" + $('#type').val() + "?questionID=" + $('#questionID').val() + "&questionNumber=" + $('#questionNumber').val() + "&pollID=" + $('#pollID').val() + "&pollName=" + $('#pollName').val() + "&saved=true";
                        }
                    });
                }
            });
        }
        else {
            $.ajax({
                type: 'POST',
                url: "Update" + $('#type').val(),
                data: $('#form0').serialize(),
                success: function(response) {
                    $.ajax({
                        type: 'POST',
                        url: "AddAnswerString",
                        data: { questionID: $('#questionID').val(), answer: $('#addOptionText').val() },
                        success: function(response) {
                            window.location.href = "Edit" + $('#type').val() + "?questionID=" + $('#questionID').val() + "&questionNumber=" + $('#questionNumber').val() + "&pollID=" + $('#pollID').val() + "&pollName=" + $('#pollName').val() + "&saved=true";
                        }
                    });
                }
            });
        }
    });
    $('.colour').each(function() {
        $(this).css("background-color", "#" + $(this).attr("id").substring(6));
        $(this).click(function() {
            $('#fontColour').val("#" + $(this).attr("id").substring(6));
            $('#showFontColour').css("background-color", "#" + $(this).attr("id").substring(6));
            $('#colourpicker').addClass("none");
        });
    });
    $('#showFontColour').click(function() {
        $('#colourpicker').removeClass("none");
    });
    $('#showChartColours').click(function() {
        $('#chartColoursPicker').removeClass("none");
    });
    $('#goyr').click(function() {
        $('#resultsChart_colours').val("green, orange, yellow, red");
        $('#chartColour1').css("background-color", "Green");
        $('#chartColour2').css("background-color", "Orange");
        $('#chartColour3').css("background-color", "Yellow");
        $('#chartColour4').css("background-color", "Red");
        $('#chartColoursPicker').addClass("none");
    });
    $('#rgoy').click(function() {
        $('#resultsChart_colours').val("red, green, orange, yellow");
        $('#chartColour1').css("background-color", "Red");
        $('#chartColour2').css("background-color", "Green");
        $('#chartColour3').css("background-color", "Orange");
        $('#chartColour4').css("background-color", "Yellow");
        $('#chartColoursPicker').addClass("none");
    });
    $('#oygr').click(function() {
        $('#resultsChart_colours').val("orange, yellow, green, red");
        $('#chartColour1').css("background-color", "Orange");
        $('#chartColour2').css("background-color", "Yellow");
        $('#chartColour3').css("background-color", "Green");
        $('#chartColour4').css("background-color", "Red");
        $('#chartColoursPicker').addClass("none");
    });
});